//
//  NStringExtension.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/13.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import Foundation
extension NSString{
    func isValidPhoneNumber()->Bool{
        let regex = "^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        return predicate.evaluateWithObject(self)
    }
    func sizewithFont(font:UIFont,maxSize:CGSize)->CGSize{
        let atrributs = [NSFontAttributeName:font]
        return self.boundingRectWithSize(maxSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: atrributs, context: nil).size
    }
}
extension String  {
    var md5: String! {
        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
        let strLen = CC_LONG(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
        
        CC_MD5(str!, strLen, result)
        
        let hash = NSMutableString()
        for i in 0..<digestLen {
            hash.appendFormat("%02x", result[i])
        }
        
        result.dealloc(digestLen)
        
        return String(format: hash as String)
    }
}